﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Model.Common;
using Chinaer.Common.Extensions;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class Country : Repository, IBll.ICountry
    {
        IDao.ICountryDAO countryDAO = new Dao.CountryDAO();
        public JsonResult Add(DTO.CountryDTO t)
        {
            JsonResult jsonResult = new JsonResult();
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            if (!string.IsNullOrEmpty(t.CountryCode))
            {
                if (countryDAO.GetByCode(t.CountryCode) != null)
                {
                    jsonResult.Success = false;
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                    jsonResult.ErrorMessage = "国家编码已经存在";
                    return jsonResult;
                }
            }
            if (!string.IsNullOrEmpty(t.CountryName))
            {
                if (countryDAO.GetByName(t.CountryName) != null)
                {
                    jsonResult.Success = false;
                    jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                    jsonResult.ErrorMessage = "国家名称已经存在";
                    return jsonResult;
                }
            }
            try
            {
                Model.Entities.Model_Country model_Country = new Model.Entities.Model_Country();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Country);
                jsonResult.Success = countryDAO.Add(model_Country);
                if (string.IsNullOrEmpty(model_Country.ID) || model_Country.ID.Trim() == "")
                {
                    model_Country.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
                }
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Success;
                jsonResult.ErrorMessage = "";
            }
            catch (Exception ex)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.Exception;
                jsonResult.ErrorMessage = Chinaer.Core.Helper.ExceptionMessageHelper.RollException(ex);
            }
            return jsonResult;
        }

        public JsonResult Delete(DTO.CountryDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            Model.Common.JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Country model_Country = new Model.Entities.Model_Country();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Country);
            jsonResult.Success = countryDAO.Delete(model_Country);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            Model.Common.JsonResult jsonResult = new JsonResult();
            jsonResult.Success = countryDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.CountryDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            Model.Common.JsonResult jsonResult = new JsonResult();
            Model.Entities.Model_Country model_Country = new Model.Entities.Model_Country();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_Country);
            jsonResult.Success = countryDAO.Update(model_Country);
            return jsonResult; 
        }

        public DTO.CountryDTO GetByID(string id)
        {
            Model.Entities.Model_Country model_Country = new Model.Entities.Model_Country();
            model_Country = countryDAO.GetByID(id);
            DTO.CountryDTO countryDTO = new DTO.CountryDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Country, countryDTO);
            return countryDTO;
        }

        public List<DTO.CountryDTO> GetAllCountry(string apiToken)
        {
            List<Model.Entities.Model_Country> listCountry = countryDAO.GetAllCountry();
            List<DTO.CountryDTO> listDTO = new List<DTO.CountryDTO>();
            foreach (Model.Entities.Model_Country item in listCountry)
            {
                DTO.CountryDTO countryDTO = new DTO.CountryDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, countryDTO);
                countryDTO.ApiToken = apiToken;
                listDTO.Add(countryDTO);
            }
            return listDTO;
        }
    }
}
